package com.pushmessgae.server.ui;

import java.awt.Component;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import com.pushmessgae.server.ClientContainer.ClientContainerListener;
import com.pushmessgae.server.ClientThread;
import com.pushmessgae.server.Server;
import com.pushmessgae.server.config.StringValue;
import com.pushmessgae.server.log.Log;
import com.pushmessgae.server.log.Log.LogInfo;
import com.pushmessgae.server.log.Log.LogListener;

public class MainFrame extends JFrame implements ActionListener , LogListener , ClientContainerListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Server server = new Server();
	
	private JPanel header,bottom;
	
	private StringValue stringValue = StringValue.getInstance();
	
	private JButton startbtn = new JButton(stringValue.getString("main.frame.startbtn"));
	
	private String [] clienttableHeaders = {stringValue.getString("client.table.headername") , 
			stringValue.getString("client.table.headerhost") ,
			stringValue.getString("client.table.headerport")};
	
	private String [] logtableHeaders = {stringValue.getString("log.table.headertime") ,
			stringValue.getString("log.table.headertag") ,
			stringValue.getString("log.table.headermessage")};
	
	private DefaultTableModel clientsJTableModel,logsJTableModel;
	
	private JTable clientsJTable,logsJTable;
	
	/** 
	 * @param args
	 */
	public MainFrame() {
		this.setTitle(stringValue.getString("main.frame.title"));
		this.setSize(800, 600);
		int height = Toolkit.getDefaultToolkit().getScreenSize().height;
		int width = Toolkit.getDefaultToolkit().getScreenSize().width;
		this.setLocation((width - this.getSize().width) / 2 , (height - this.getSize().height) / 2 );
		builderUI();
		Log.getInstance().addLogListener(this);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	
	private void builderUI(){
		header = new JPanel(new GridLayout(2, 1));
		bottom = new JPanel();
		addRoot(header);
		addRoot(bottom);
		
		addClientTable();
		addControlPanl();
		
		addLogsFilter();
		addLogsTable();
		
	}
	
	private void addRoot(Component component){
		this.setLayout(new GridLayout(2, 1));
		this.add(component);
	}
	
	private void addClientTable(){
		
		server.getClientContainer().setContainerListener(this);
		
		clientsJTableModel = new DefaultTableModel(new String [][]{}, clienttableHeaders);
		clientsJTable = new JTable(clientsJTableModel);
		JScrollPane clientsJTableJScrollPane = new JScrollPane(clientsJTable);
		addHeader(clientsJTableJScrollPane);
	}
	
	private void addControlPanl(){
		JPanel jPanel = new JPanel();
		startbtn  = new JButton(stringValue.getString("main.frame.startbtn"));
		startbtn.addActionListener(this);
		jPanel.add(startbtn);
		addHeader(jPanel);
	}
	
	private void addLogsFilter(){
		JTextField field = new JTextField();
//		field.setHighlighter()
		addBottom(field);
	}
	
	private void addLogsTable(){
		logsJTableModel = new DefaultTableModel(new String [][]{}, logtableHeaders);
		logsJTable  = new JTable(logsJTableModel);
		JScrollPane logsJTableModelJScrollPane = new JScrollPane(logsJTable);
		addBottom(logsJTableModelJScrollPane);
	}
	
	private void addHeader(Component component){
		if(null != header){
			header.setLayout(new GridLayout(1, 2));
			header.add(component);
		}
	}
	
	private void addBottom(Component component){
		if(null != bottom){
			bottom.setLayout(new BoxLayout(bottom , BoxLayout.Y_AXIS));
			bottom.add(component);
		}
	}
	
	

	@Override
	public void actionPerformed(ActionEvent e) {
		if(server.isStop()){
			server.start();
			startbtn.setText(stringValue.getString("main.frame.stoptbtn"));
		}else {
			server.stop();
			startbtn.setText(stringValue.getString("main.frame.startbtn"));
		}
	}

	@Override
	public void onLog(LogInfo logInfo) {
		final int lastrow = logsJTable.getRowCount();
		logsJTableModel.insertRow(lastrow ,new String []{logInfo.getTime() , logInfo.getTag() , logInfo.getMessage()});
	}

	@Override
	public void addClient(ClientThread clientThread) {
		final int lastrow = clientsJTableModel.getRowCount();
		clientsJTableModel.insertRow(lastrow, new String [] {clientThread.getClientInfo() , clientThread.getAddress() , clientThread.getPort()});
	}

	@Override
	public void removeClient(ClientThread clientThread) {
		int row = 0;
		int rows = clientsJTableModel.getRowCount();
		for(int i = 0 ; i < rows ; i ++){
			if(clientsJTableModel.getValueAt(i, 0).equals(clientThread.getClientInfo())){
				row = i;
				break;
			}
		}
		clientsJTableModel.removeRow(row );
	}

}
